package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Stream;

/**
 * @FileName: HospitalServiceImpl
 * @Author Steven
 * @Date: 2021/3/18
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    Logger log = LoggerFactory.getLogger(HospitalServiceImpl.class);

    @Autowired
    HospitalRepository hospitalRepository;

    @Autowired
    DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        log.info(JSONObject.toJSONString(paramMap));
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        //判断是否存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (null != targetHospital) {
            hospital.setStatus(targetHospital.getStatus());

            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
        else{
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Page<Hospital> selectPage(Integer current, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort=Sort.by(Sort.Direction.DESC,"create_time");
        Pageable pageable=PageRequest.of(current-1, limit,sort);
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example example=Example.of(hospital,matcher);
        Page pages = hospitalRepository.findAll(example, pageable);
        pages.getContent().stream().forEach(item ->
            this.packHospital((Hospital) item)
        );
        return pages;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        if(status==0 || status==1){
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> show(String id) {
        Map<String,Object> hashMap=new HashMap<>();
        Hospital hospital = this.packHospital(hospitalRepository.findById(id).get());
        hashMap.put("hospital",hospital);
        hashMap.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        return hashMap;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if(null!=hospital){
            return hospital.getHosname();
        }
        return null;
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public List<Hospital> findByHosName(String hosName) {
        List<Hospital> hospitalList =hospitalRepository.getByHosnameLike(hosName);
        return hospitalList;
    }

    @Override
    public Map<String, Object> item(String hosCode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hosCode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    private Hospital packHospital(Hospital hospital){
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        hospital.getParam().put("hostypeName",hostypeString);
        hospital.getParam().put("findAllAddress",provinceString+cityString);
        return hospital;
    }

    /**
     * 获取查询list集合，遍历进行医院等级封装
     * @param hospital
     * @return
     */
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }

}
